package com.example.node.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.node.R;
import com.example.node.bean.MovieListBean;
import com.example.node.listener.OnClick;
import com.example.node.utils.GlideUtils;
import java.util.List;

public class MovieListAdapter extends RecyclerView.Adapter<MovieListAdapter.ViewHolder> {

    private Context mContext;
    private List<MovieListBean> mList;
    private OnClick onClick;

    public MovieListAdapter(List<MovieListBean> mList) {
        this.mList = mList;
    }

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mContext=viewGroup.getContext();
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_movie_list_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        MovieListBean movieListBean = mList.get(i);
        viewHolder.tv_title.setText(movieListBean.getTitle());
        viewHolder.tv_sub_text.setText(movieListBean.getSubtitle());
        GlideUtils.loadImageView(mContext,movieListBean.getImg_url(),viewHolder.iv_movie_bg);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClick.onClick(i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_sub_text, tv_title;
        private ImageView iv_movie_bg;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_sub_text = itemView.findViewById(R.id.tv_sub_text);
            tv_title = itemView.findViewById(R.id.tv_title);
            iv_movie_bg = itemView.findViewById(R.id.iv_movie_bg);
        }
    }
}
